package com.zjm.service.impl;

import com.zjm.dao.*;
import com.zjm.entity.*;
import com.zjm.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class TeacherServiceImpl implements TeacherService {

    //自动注入
    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private VacateDao vacateDao;

    @Autowired
    private MajorsDao majorsDao;

    @Autowired
    private ClasDao clasDao;

    @Autowired
    private ChangeMajorDao changeMajorDao;

    @Override
    public Map<String ,Object> register(Teacher teacher , String confirmPassword) {
        Map<String,Object> map = new HashMap<>();
        if(teacher.getId()==null){
            map.put("msg","账号不能为空");
            return  map;
        }
        if(teacher.getPassword()==null){
            map.put("msg","密码不能为空");
            return  map;
        }
        if(confirmPassword==null){
            map.put("msg","确认密码不能为空");
            return  map;
        }
        if(!teacher.getPassword().equals(confirmPassword)){
            map.put("msg","两次密码不相同");
            return  map;
        }
        if(teacher.getName()==null){
            map.put("msg","姓名不能为空");
            return  map;
        }
        if(teacher.getAcademy()==null){
            map.put("msg","学院不能为空");
            return  map;
        }
        if(teacher.getMajor()==null){
            map.put("msg","专业不能为空");
            return  map;
        }
        Teacher tea = teacherDao.selectTeacherById(teacher.getId());
        if(tea!=null){
            map.put("msg","用户已存在");
            return map;
        }
        int num = teacherDao.insertTeacher(teacher);
        return map;
    }

    @Override
    public Teacher login(Integer id,String password) {
        if ((id+"")==null || password==null)
            return null;
        Teacher teacher = teacherDao.selectTeacherById(id);
        if(teacher.getPassword().equals(password)){
            return teacher;
        }
        return null;
    }

    @Override
    public Teacher showInfo(Integer teacher_id) {
        return null;
    }

    @Override
    public Map<String, String> publishClas(Clas clas) {
        Map<String,String> map = new HashMap<>();
        map.put("msg","注入成功");
        clasDao.insertClas(clas);
        return map;
    }

    @Override
    public List<Clas> showMyClas(Integer teacher_id) {
        List<Clas> clasList = clasDao.selectTeacherClas(teacher_id);
        return clasList;
    }

    @Override
    public List<Select_clas> showMyClasStudents(Integer clas_id) {
        if(clas_id !=null){
            List<Select_clas> select_clas = clasDao.SelectClasStudents(clas_id);
            return select_clas;
        }
        return null;
    }

    @Override
    public Map<String, Object> publishClasScore(Clas clas) {
        return null;
    }

    @Override
    public Map<String, Object> publishScore(List<Select_clas> select_clas) {
        return null;
    }

    @Override
    public Map<String, Integer> showNumber(Integer teacher_id) {
        return null;
    }

    @Override
    public List<Vacate> showVacates(Integer teacher_id) {
        List<Vacate> vacateList= vacateDao.selectTeacherVacate(teacher_id);
        return vacateList;
    }

    @Override
    public void passVacate(Integer student_id , String pass , String reason) {
        Integer vacate_id = vacateDao.selectOneVacate(student_id,reason);
        vacateDao.updateVacatePass(vacate_id,pass);
    }

    @Override
    public List<Change_major> showChangeMajor(Teacher teacher) {
        if("辅导员".equals(teacher.getJob())){
            List<Change_major> changeMajorList = changeMajorDao.selectCounsellorChangeMajor(teacher);
            return changeMajorList;
        }else if ("院长".equals(teacher.getJob())){
            List<Change_major> changeMajorList = changeMajorDao.selectPresidentChangeMajor(teacher);
            return changeMajorList;
        }
        return null;
    }


    @Override
    public void passChangeMajor(Integer student_id,String pass,Teacher teacher) {
        Change_major change_major = changeMajorDao.selectOnChangeMajor(student_id);
        if(pass.equals("拒绝"))changeMajorDao.updatePassFasle(student_id);
        else {
            if("辅导员".equals(teacher.getJob())){
                System.out.println("辅导员");
                if(change_major.getOut_counselor_pass()==null){
                    boolean b = changeMajorDao.updateOutCounsellorPass(student_id , teacher.getMajor() , pass);
                    System.out.println(b);
                    System.out.println("转出通过");
                }
                else{
                    System.out.println("转入通过");
                    changeMajorDao.updateInCounsellorPass(student_id,teacher.getMajor(),pass); }
            }else if ("院长".equals(teacher.getJob())){
                System.out.println("院长");
                if(change_major.getOut_president_pass()==null){
                    boolean x = changeMajorDao.updateOutPresidentPass(student_id,teacher.getAcademy(),pass);
                    System.out.println(x);
                    System.out.println("转出通过");
                }
                else{
                    System.out.println("转入通过");
                    changeMajorDao.updateInPresidentPass(student_id,teacher.getAcademy(),pass); }
            }
        }
        change_major = changeMajorDao.selectChangeMajor(student_id);
        if ("批准".equals(change_major.getIn_counselor_pass())) studentDao.updateStudentMajor(student_id,change_major.getIn_major(),change_major.getIn_academy());
    }

    @Override
    public void updateScore(Integer clas_id,Integer student_id,Double score){
        clasDao.updateClasScore(student_id,clas_id,score);
    }

    /*@Override
    public List<DemoData> showStudentList(Integer clas_id){
        List<DemoData> demoDataList= clasDao.SelectStudents(clas_id);
        return  demoDataList;
    }*/

    @Override
    public void updateScoreList(List<DemoData> list){
        /*for (DemoData demoData:list){
            if(demoData !=null &&demoData.getStudent_id()!=null && demoData.getClas_id()!=null && demoData.getScore()!=null)
            clasDao.updateClasScore(demoData.getStudent_id(),demoData.getClas_id(),demoData.getScore());
        }*/
        clasDao.batchUpdate(list);
    }
}
